package com.toidep.fragment.mains;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.VideoAdapter;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.interfaces.IFragmentRefreshable;
import com.toidep.widgets.sectionlist.AmazingListView;

public class VideoFragment extends BaseFragment implements OnClickListener, IFragmentRefreshable{

	private String TAG = "MainFragment";
	
	public boolean isRootFragment = true;
	private MainActivity mainActivity;
	private View view;
	
	private RelativeLayout buttonNew, buttonHot, buttonTop, buttonAll;
	private RelativeLayout layoutNew, layoutHot, layoutTop, layoutAll;
	
	private AmazingListView listVideosNew, listVideosHot, listVideosTop, listVideosAll;
	private VideoAdapter adapterVideosNew, adapterVideosHot, adapterVideosTop, adapterVideosAll;

	private boolean isRequestedNewData = false;
	private boolean isRequestedHotData = false;
	private boolean isRequestedTopData = false;
	private boolean isRequestedAllData = false;
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_VIDEO;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_video_detail));
		mainActivity.mainFragment.showButtonSlide(true);
		
		isRequestedNewData = false;
		isRequestedHotData = false;
		isRequestedTopData = false;
		isRequestedAllData = false;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_video, container, false);

		mainActivity = (MainActivity) getActivity();
		
		initComponents();
		setBaseValues();
		switchMenuVideo(Constants.TAB_MODE_NEW);
		
		
		
		return view;
	}

	private void initComponents() {
		
		buttonNew = (RelativeLayout) view.findViewById(R.id.layoutButtonNewOfVideos);
		buttonNew.setOnClickListener(this);
		buttonHot = (RelativeLayout) view.findViewById(R.id.layoutButtonHotOfVideos);
		buttonHot.setOnClickListener(this);
		buttonTop = (RelativeLayout) view.findViewById(R.id.layoutButtonTopOfVideos);
		buttonTop.setOnClickListener(this);
		buttonAll = (RelativeLayout) view.findViewById(R.id.layoutButtonAllOfVideos);
		buttonAll.setOnClickListener(this);
		
		layoutNew = (RelativeLayout) view.findViewById(R.id.layoutVideoNew);
		layoutHot = (RelativeLayout) view.findViewById(R.id.layoutVideoHot);
		layoutTop = (RelativeLayout) view.findViewById(R.id.layoutVideoTop);
		layoutAll = (RelativeLayout) view.findViewById(R.id.layoutVideoAll);
		
		// Khoi tao list model NEW
		listVideosNew = (AmazingListView) view.findViewById(R.id.listVideoNew);
		listVideosNew.setAdapter(adapterVideosNew = new VideoAdapter(this, Constants.CATEGORY_VIDEO, Constants.TAB_MODE_NEW));
		listVideosNew.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		adapterVideosNew.setAmazingListView(listVideosNew, ConfigUtility.PAGE_SIZE);
		adapterVideosNew.notifyMayHaveMorePages();
		
		// Chuyen toi detail cua Video
		listVideosNew.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (adapterVideosNew != null && position >= 0) {
					adapterVideosNew.onItemClick(position);
				}
			}
		});
		
		// Khoi tao list model HOT
		listVideosHot = (AmazingListView) view.findViewById(R.id.listVideoHot);
		listVideosHot.setAdapter(adapterVideosHot = new VideoAdapter(this, Constants.CATEGORY_VIDEO, Constants.TAB_MODE_HOT));
		listVideosHot.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		adapterVideosHot.setAmazingListView(listVideosHot, ConfigUtility.PAGE_SIZE);
		adapterVideosHot.notifyMayHaveMorePages();
		
		// Chuyen toi detail cua Video
		listVideosHot.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (adapterVideosHot != null && position >= 0) {
					adapterVideosHot.onItemClick(position);
				}
			}
		});
		
		// Khoi tao list model TOP
		listVideosTop = (AmazingListView) view.findViewById(R.id.listVideoTop);
		listVideosTop.setAdapter(adapterVideosTop = new VideoAdapter(this, Constants.CATEGORY_VIDEO, Constants.TAB_MODE_TOP));
		listVideosTop.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		adapterVideosTop.setAmazingListView(listVideosTop, ConfigUtility.PAGE_SIZE);
		adapterVideosTop.notifyMayHaveMorePages();

		// Chuyen toi detail cua Video
		listVideosTop.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (adapterVideosTop != null && position >= 0) {
					adapterVideosTop.onItemClick(position);
				}
			}
		});
		
		// Khoi tao list model ALL
		listVideosAll = (AmazingListView) view.findViewById(R.id.listVideoAll);
		listVideosAll.setAdapter(adapterVideosAll = new VideoAdapter(this, Constants.CATEGORY_VIDEO, Constants.TAB_MODE_ALL));
		listVideosAll.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		adapterVideosAll.setAmazingListView(listVideosAll, ConfigUtility.PAGE_SIZE);
		adapterVideosAll.notifyMayHaveMorePages();

		// Chuyen toi detail cua Video
		listVideosAll.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (adapterVideosAll != null && position >= 0) {
					adapterVideosAll.onItemClick(position);
				}
			}
		});
	}
	
	public static VideoFragment newInstance() {
		VideoFragment myFragment = new VideoFragment();

		myFragment.setRootFragment(true);
		return myFragment;
	}
	
	public void switchMenuVideo(int tabId) {
		
		switch (tabId) {
		case Constants.TAB_MODE_NEW:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_red_normal);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.VISIBLE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.GONE);
			
			// Request data (if needed)
			if (adapterVideosNew == null || adapterVideosNew.getCount() <= 0)
				requestData(Constants.TAB_MODE_NEW);
			
			break;
			
		case Constants.TAB_MODE_HOT:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_red_normal);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.VISIBLE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.GONE);
			
			// Request data (if needed)
			if (adapterVideosHot == null || adapterVideosHot.getCount() <= 0)
				requestData(Constants.TAB_MODE_HOT);
			break;
			
		case Constants.TAB_MODE_TOP:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_red_normal);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.VISIBLE);
			layoutAll.setVisibility(View.GONE);
			
			// Request data (if needed)
			if (adapterVideosTop == null || adapterVideosTop.getCount() <= 0)
				requestData(Constants.TAB_MODE_TOP);
			break;
			
		case Constants.TAB_MODE_ALL:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_red_normal);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.VISIBLE);
			
			// Request data (if needed)
			if (adapterVideosAll == null || adapterVideosAll.getCount() <= 0)
				requestData(Constants.TAB_MODE_ALL);
			break;

		default:
			break;
		}
		setPaddingAllButton();
		
	}
	
	private void setPaddingAllButton() {
		int paddingDP = (int)(10 * ConfigUtility.scale);
		buttonNew.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonHot.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonTop.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonAll.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
	}
	
	/**
	 * Goi API get list models
	 */
	private void requestData(int typeVideoList) {
		
		switch (typeVideoList) {
		case Constants.TAB_MODE_NEW:
			if (isRequestedNewData)
				return;
			
			isRequestedNewData = true;
			adapterVideosNew.resetList();
			break;
			
		case Constants.TAB_MODE_HOT:
			if (isRequestedHotData)
				return;
			
			isRequestedHotData = true;
			adapterVideosHot.resetList();
			break;
			
		case Constants.TAB_MODE_TOP:
			if (isRequestedTopData)
				return;
			
			isRequestedTopData = true;
			adapterVideosTop.resetList();
			break;
			
		case Constants.TAB_MODE_ALL:
			if (isRequestedAllData)
				return;
			
			isRequestedAllData = true;
			adapterVideosAll.resetList();
			break;

		default:
			break;
		}
		
	}

	@Override
	public void onClick(View v) {

		if (v == buttonNew) {
			switchMenuVideo(Constants.TAB_MODE_NEW);
		} else if (v == buttonHot) {
			switchMenuVideo(Constants.TAB_MODE_HOT);
		} else if (v == buttonTop) {
			switchMenuVideo(Constants.TAB_MODE_TOP);
		} else if (v == buttonAll) {
			switchMenuVideo(Constants.TAB_MODE_ALL);
		}
	}

	@Override
	public void refreshData() {
		if (adapterVideosNew.getCount() > 0) {
			adapterVideosNew.resetList();
		}
		if (adapterVideosHot.getCount() > 0) {
			adapterVideosHot.resetList();
		}
		if (adapterVideosTop.getCount() > 0) {
			adapterVideosTop.resetList();
		}
		if (adapterVideosAll.getCount() > 0) {
			adapterVideosAll.resetList();
		}
	}

}
